/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.core.beActionAssemblerFactory;

import com.inspur.edp.bef.bizentity.operation.BizAction;
import com.inspur.edp.bef.dtgenerator.api.beActionAssemblerGen.InternalBEActionMethodGenerator;
import com.inspur.edp.bef.dtgenerator.core.BefCoreGenCtx;
import com.inspur.edp.bef.dtgenerator.core.beActionAssembler.InternalBEActionAssGenerator;
import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import java.lang.reflect.Field;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.ReturnStatement;

public class InternalBEActionMethodImplGenerator extends ClassMethodGenerator {
  private BefCoreGenCtx coreGenContext;
  private BizAction action;

  public InternalBEActionMethodImplGenerator(BefCoreGenCtx coreGenContext, BizAction action) {
    this.coreGenContext = coreGenContext;
    this.action = action;
  }

  @Override
  protected boolean getIsOverride() {
    return true;
  }

  @Override
  protected java.util.ArrayList<ModifierKeyword> getAccessModifiers() {
    return new java.util.ArrayList<ModifierKeyword>(
        java.util.Arrays.asList(ModifierKeyword.PUBLIC_KEYWORD));
  }

  @Override
  protected String getMethodName() {
    return InternalBEActionMethodGenerator.getMethodName(action);
  }

  @Override
  protected java.util.ArrayList<ParameterInfo> getParameterCollection() {
    return InternalBEActionMethodGenerator.getMethodParameters(action);
  }

  @Override
  protected TypeInfo getReturnType() {
    return InternalBEActionMethodGenerator.getReturnType(action);
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    ClassInstanceCreation creationExpression = ast.newClassInstanceCreation();
    creationExpression.setType(ast.newSimpleType(ast.newSimpleName(getAssemblerClassName(action))));
    for (ParameterInfo formalPar : InternalBEActionMethodGenerator.getMethodParameters(action)) {
        creationExpression.arguments().add(ast.newSimpleName(formalPar.getParamName()));
    }
    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(creationExpression);
    block.statements().add(returnStatement);
    return block;
  }

  private String getAssemblerClassName(BizAction action) {
    return InternalBEActionAssGenerator.getItemName(action);
  }

  @Override
  protected java.util.ArrayList<BaseGenerator> createChildGenerators() {
    java.util.ArrayList<BaseGenerator> result = new java.util.ArrayList<BaseGenerator>(1);
    result.add(new InternalBEActionAssGenerator(coreGenContext, action));
    return result;
  }
}
